﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;
using MapCenterSystem.Utility;
using MapCenterSystem.DefineType;
using System.Collections.Generic;
using System.Globalization;
using System.Drawing;


using System.Threading;
using System.Resources;

namespace MobiMap.Guest
{
    public partial class Direction : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;
        string direction = string.Empty;
        string turnright = string.Empty;
        string turnleft = string.Empty;
        string gostraight = string.Empty;
        string distanceLanguage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Language
            SetLanguage();

            //Title
            Direction_title.Text = direction;

            //Chinh giao dien: dua TOOLBAR len vi tri TOP
            //Panel divEdge = (Panel)Master.FindControl("divEdge");
            //divEdge.Style["display"] = "none";

            HtmlControl divToolbarWrapper = (HtmlControl)Master.FindControl("divToolbarWrapper");         
            divToolbarWrapper.Style["top"] = "6px";

            //LiteralControl divNewEdge = new LiteralControl();
            //divNewEdge.Text = @"<div class='toolbar_edge'><div><img id='idNewEdge' src='../Image/Collapse.png' onclick='ShowHideToolbar()' alt='edge'/></div></div>";
            //divToolbarWrapper.Controls.Add(divNewEdge);

            HtmlControl divToolbar = (HtmlControl)Master.FindControl("divToolbar");
            divToolbar.Style["display"] = "none";

            //Direction
            List<GraphNodeInfoEx> lstGraphNode = new List<GraphNodeInfoEx>();
            if (Session["mobimap_lstGraphNode"] != null)
            {
                lstGraphNode = (List<GraphNodeInfoEx>)Session["mobimap_lstGraphNode"];
                double angle;
                PointF A, B, C;
                string orientation = gostraight;
                string streetName = String.Empty;

                streetName = StreetEx.GetFullNameById(lstGraphNode[1].IDStreet);
                AddDirection(0, orientation, streetName);


                double distance = Coordinate.Distance(lstGraphNode[0].Location, lstGraphNode[1].Location);
                for(int i=1; i<lstGraphNode.Count - 1; i++)
                {
                    distance += Coordinate.Distance(lstGraphNode[i].Location, lstGraphNode[i +1].Location);
                    if (lstGraphNode[i].IDStreet == lstGraphNode[i+1].IDStreet)
                    {                           
                        continue;
                    }
                    else
                    {
                        A = new PointF((float)lstGraphNode[i - 1].Location.Longitude,
                                                (float)lstGraphNode[i - 1].Location.Latitude);
                        B = new PointF((float)lstGraphNode[i].Location.Longitude,
                                                (float)lstGraphNode[i].Location.Latitude);
                        C = new PointF((float)lstGraphNode[i + 1].Location.Longitude,
                                                (float)lstGraphNode[i + 1].Location.Latitude);

                        System.Windows.Vector AB = new System.Windows.Vector(B.X - A.X, B.Y - A.Y);
                        System.Windows.Vector BC = new System.Windows.Vector(C.X - B.X, C.Y - B.Y);
                        angle = System.Windows.Vector.AngleBetween(AB, BC);


                        if (angle < 0)
                            orientation = turnright;
                        else
                            orientation = turnleft; 
                    }

                    streetName = StreetEx.GetFullNameById(lstGraphNode[i + 1].IDStreet);
                    AddDirection(i, orientation, streetName);
                    lbDistance.Text = distanceLanguage + ": " + distance.ToString("0.000", CultureInfo.InstalledUICulture) + " km";
                }
            }
            else
            {
                Response.Redirect("Home.aspx");
            }            
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            HtmlImage imgLanguage = (HtmlImage)Master.FindControl("idLanguage");
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                imgLanguage.Src = "../Image/" + language + ".png";
                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
            imgLanguage.Src = "../Image/" + "vi-VN" + ".png";
        }

        public void LoadString(CultureInfo ci)
        {
            direction = rm.GetString("Info_linkDirection", ci);
            gostraight = rm.GetString("Direction_GoStraight", ci);
            turnleft = rm.GetString("Direction_TurnLeft", ci);
            turnright = rm.GetString("Direction_TurnRight", ci);
            distanceLanguage = rm.GetString("Direction_Distance", ci);
        }

        public void AddDirection(int rowIdx, string orientation, string streetName)
        {
            Panel panel = new Panel();
            panel.CssClass = "rowInfo";
            if (rowIdx % 2 == 0)
                panel.Style["background-color"] = "#edf5db";

            Label lb = new Label();
            lb.Text = orientation + streetName;
            panel.Controls.Add(lb);
            listDirection.Controls.Add(panel);
        }        
    }
}
